/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
 */

package com.huawei.vo;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 封装统一返回实体
 *
 * @since 2025.10.11
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResponse {
    private boolean success;
    private Object data;
    private String message;
    private Integer code;
    private Long timestamp;

    private ApiResponse(boolean success, Object data, String message, Integer code) {
        this.success = success;
        this.data = data;
        this.message = message;
        this.code = code;
        this.timestamp = System.currentTimeMillis();
    }

    public static ApiResponse success(Object data, Integer code) {
        return new ApiResponse(true, data, "请求成功", code);
    }

    public static ApiResponse success(Object data) {
        return success(data, 200);
    }

    public static ApiResponse error(String message, Integer code) {
        return new ApiResponse(false, null, message, code);
    }

    public static ApiResponse error(String message) {
        return error(message, 500);
    }

    // getters and setters
    public boolean isSuccess() { return success; }
    public void setSuccess(boolean success) { this.success = success; }

    public Object getData() { return data; }
    public void setData(Object data) { this.data = data; }

    public String getMessage() { return message; }
    public void setMessage(String message) { this.message = message; }

    public Integer getCode() { return code; }
    public void setCode(Integer code) { this.code = code; }

    public Long getTimestamp() { return timestamp; }
    public void setTimestamp(Long timestamp) { this.timestamp = timestamp; }

    @Override
    public String toString() {
        return String.format("ApiResponse{success=%s, data=%s, message='%s', code=%d}",
            success, data, message, code);
    }
}